<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item label="活动类型" prop="typeId">
            <el-input
              v-if="false"
              v-model="form.typeId"
              style="width: 370px;"
            />
            <div v-if="form.typeId">
              {{ dialogTableDataTypeId.showInputValue ? dialogTableDataTypeId.showInputValue : form.typeName }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataTypeId"
              @select-callback="dialogTableSelectedCallBackTypeId"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="活动编码" prop="activityCode">
            <el-input v-model="form.activityCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动标题" prop="activityTitle">
            <el-input v-model="form.activityTitle" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动主图" prop="activityFirstFigure">
              <MaterialList
                      v-model="activityFirstFigureArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="活动详情介绍">
            <el-input :rows="3" v-model="form.activityDetail" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动详情图（多张图片拼接存储）">
              <MaterialList
                      v-model="activityDetailFigureArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="报名开始时间" prop="activityApplyStartTime">
            <el-date-picker v-model="form.activityApplyStartTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="报名截止时间" prop="activityApplyEndTime">
            <el-date-picker v-model="form.activityApplyEndTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动开始日期" prop="activityStartDate">
            <el-date-picker v-model="form.activityStartDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动结束日期" prop="activityEndDate">
            <el-date-picker v-model="form.activityEndDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="女生人数" prop="maxNumberG">
            <el-input v-model="form.maxNumberG" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="男生人数" prop="maxNumberB">
            <el-input v-model="form.maxNumberB" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动微信群二维码">
            <el-input v-model="form.activityWechatGroup" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="活动状态" prop="activityStatus">
            <el-select v-model="form.activityStatus" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.ACTIVITY_STATUS"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="男生费用" prop="activityCostB">
            <el-input v-model="form.activityCostB" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="女生费用" prop="activityCostG">
            <el-input v-model="form.activityCostG" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="费用类型" prop="activityCostType">
            <el-input v-model="form.activityCostType" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="省市区" prop="areaId">
            <el-input
              v-if="false"
              v-model="form.areaId"
              style="width: 370px;"
            />
            <div v-if="form.areaId">
              {{ dialogTableDataAreaId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataAreaId"
              @select-callback="dialogTableSelectedCallBackAreaId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="活动详细地点">
            <el-input v-model="form.activityAddressDetail" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="出行方式">
            <el-select v-model="form.activityTripMode" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.ACTIVITY_TRIP_MODE"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="活动负责人" prop="activityPrincipalMember">
            <el-input v-model="form.activityPrincipalMember" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="小组奖金规则" prop="distributionIdTeam">
            <el-input
              v-if="false"
              v-model="form.distributionIdTeam"
              style="width: 370px;"
            />
            <div v-if="form.distributionIdTeam">
              {{ dialogTableDataDistributionIdTeam.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataDistributionIdTeam"
              @select-callback="dialogTableSelectedCallBackDistributionIdTeam"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="合伙人奖金规则" prop="distributionIdTeamPartner">
            <el-input
              v-if="false"
              v-model="form.distributionIdTeamPartner"
              style="width: 370px;"
            />
            <div v-if="form.distributionIdTeamPartner">
              {{ dialogTableDataDistributionIdTeamPartner.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataDistributionIdTeamPartner"
              @select-callback="dialogTableSelectedCallBackDistributionIdTeamPartner"
            ></dialogTable>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('activityTitle')" prop="activityTitle" label="活动标题" />
        <el-table-column v-if="columns.visible('typeName')" prop="typeName" label="活动类型" />
        <el-table-column v-if="columns.visible('activityCode')" prop="activityCode" label="活动编码" />
        <el-table-column v-if="columns.visible('activityStatus')" prop="activityStatus" label="活动状态">
          <template slot-scope="scope">
            {{ dict.label.ACTIVITY_STATUS[scope.row.activityStatus] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('activityDetail')" prop="activityDetail" label="活动介绍" />

        <el-table-column v-if="columns.visible('areaName')" prop="areaName" label="活动地点" />
        <el-table-column v-if="columns.visible('activityAddressDetail')" prop="activityAddressDetail" label="详细地点" />
        <el-table-column v-if="columns.visible('activityPrincipalMemberName')" prop="activityPrincipalMemberName" label="负责人" />
        <el-table-column v-if="columns.visible('activityPrincipalMemberPhone')" prop="activityPrincipalMemberPhone" label="电话" />

        <el-table-column v-if="columns.visible('activityApplyStartTime')" prop="activityApplyStartTime" label="报名时间从">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.activityApplyStartTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('activityApplyEndTime')" prop="activityApplyEndTime" label="报名时间至">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.activityApplyEndTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('activityStartDate')" prop="activityStartDate" label="活动开始">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.activityStartDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('activityEndDate')" prop="activityEndDate" label="活动结束">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.activityEndDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('activityCostType')" prop="activityCostType" label="费用类型">
          <template slot-scope="scope">
            {{ dict.label.ACTIVITY_COST_TYPE[scope.row.activityCostType] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('activityCostB')" prop="activityCostB" label="男生费用" />
        <el-table-column v-if="columns.visible('activityCostG')" prop="activityCostG" label="女生费用" />
        <el-table-column v-if="columns.visible('maxNumberG')" prop="maxNumberG" label="女生人数" />
        <el-table-column v-if="columns.visible('maxNumberB')" prop="maxNumberB" label="男生人数" />
        <el-table-column v-if="columns.visible('activityTripMode')" prop="activityTripMode" label="出行方式">
          <template slot-scope="scope">
            {{ dict.label.ACTIVITY_TRIP_MODE[scope.row.activityTripMode] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('distributionName')" prop="distributionName" label="小组规则" />
        <el-table-column v-if="columns.visible('distributionNamePartner')" prop="distributionNamePartner" label="合伙人规则" />
        <el-table-column v-if="columns.visible('activityWechatGroup')" prop="activityWechatGroup" label="群二维码" />
        <el-table-column v-if="columns.visible('activityFirstFigure')" prop="activityFirstFigure" label="活动主图">
          <template slot-scope="scope">
            <a :href="scope.row.activityFirstFigure" style="color: #42b983" target="_blank">
              <img :src="scope.row.activityFirstFigure" alt="点击打开" class="table-img" />
            </a>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('activityDetailFigure')" prop="activityDetailFigure" label="详情">
          <template slot-scope="scope">
            <a :href="scope.row.activityDetailFigure" style="color: #42b983" target="_blank">
              <img :src="scope.row.activityDetailFigure" alt="点击打开" class="table-img" />
            </a>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','tanzerActivity:edit','tanzerActivity:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerActivity from '@/api/tanzer/tanzerActivity'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable/index.vue";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '活动', url: 'api/tanzerActivity', sort: 'id,desc', crudMethod: { ...crudTanzerActivity }})
const defaultForm = { id: null, typeId: null, activityCode: null, activityTitle: null, activityFirstFigure: null, activityDetail: null, activityDetailFigure: null, activityApplyStartTime: null, activityApplyEndTime: null, activityStartDate: null, activityEndDate: null, maxNumberG: null, maxNumberB: null, activityWechatGroup: null, activityStatus: null, activityCost: null, activityCostType: null, areaId: null, activityAddressDetail: null, activityTripMode: null, activityPrincipalMember: null, distributionIdTeam: null, distributionIdTeamPartner: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'TanzerActivity',
  components: {dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['ACTIVITY_STATUS', 'ACTIVITY_COST_TYPE', 'ACTIVITY_TRIP_MODE'],
  data() {
    return {
      activityFirstFigureArr:[],activityDetailFigureArr:[],
      permission: {
        add: ['admin', 'tanzerActivity:add'],
        edit: ['admin', 'tanzerActivity:edit'],
        del: ['admin', 'tanzerActivity:del']
      },
      rules: {
        typeId: [
          { required: true, message: '活动类型ID不能为空', trigger: 'blur' }
        ],
        activityCode: [
          { required: true, message: '活动编码不能为空', trigger: 'blur' }
        ],
        activityTitle: [
          { required: true, message: '活动标题不能为空', trigger: 'blur' }
        ],
        activityFirstFigure: [
          { required: true, message: '活动主图不能为空', trigger: 'blur' }
        ],
        activityApplyStartTime: [
          { required: true, message: '报名开始时间不能为空', trigger: 'blur' }
        ],
        activityApplyEndTime: [
          { required: true, message: '报名截止时间不能为空', trigger: 'blur' }
        ],
        activityStartDate: [
          { required: true, message: '活动开始日期不能为空', trigger: 'blur' }
        ],
        activityEndDate: [
          { required: true, message: '活动结束日期不能为空', trigger: 'blur' }
        ],
        maxNumberG: [
          { required: true, message: '女生人数不能为空', trigger: 'blur' }
        ],
        maxNumberB: [
          { required: true, message: '男生人数不能为空', trigger: 'blur' }
        ],
        activityStatus: [
          { required: true, message: '活动状态不能为空', trigger: 'blur' }
        ],
        activityCost: [
          { required: true, message: '活动费用不能为空', trigger: 'blur' }
        ],
        activityCostType: [
          { required: true, message: '费用类型不能为空', trigger: 'blur' }
        ],
        activityPrincipalMember: [
          { required: true, message: '活动负责人不能为空', trigger: 'blur' }
        ],
        distributionIdTeam: [
          { required: true, message: '小组奖金规则id不能为空', trigger: 'blur' }
        ],
        distributionIdTeamPartner: [
          { required: true, message: '合伙人奖金规则id不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: '表ID，主键，供其他表做外键' },
        { key: 'typeId', display_name: '活动类型ID' },
        { key: 'activityCode', display_name: '活动编码' },
        { key: 'activityTitle', display_name: '活动标题' },
        { key: 'activityStatus', display_name: '活动状态' },
        { key: 'activityCost', display_name: '活动费用' },
        { key: 'activityCostType', display_name: '费用类型' },
        { key: 'areaId', display_name: '活动地点省市区' },
        { key: 'activityTripMode', display_name: '出行方式' },
        { key: 'activityPrincipalMember', display_name: '活动负责人' },
        { key: 'distributionIdTeam', display_name: '小组奖金规则id' }
      ],

      //自定义lov数据源
      dialogTableDataTypeId: {
        title: "活动类型",
        dialogVisible: false,
        url: "api/tanzerActivityType",
        sort: "id,desc",
        buttonContext: "选择活动类型",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'typeName', display_name: '名称' },
          { key: 'typeCode', display_name: '编码' },
        ],
        elTtableColumns: [
          {
            columnProp: "typeName",
            columnLabel: "名称"
          },
          {
            columnProp: "typeCode",
            columnLabel: "编码"
          },
        ]
      },
      dialogTableDataAreaId: {
        title: "省市区",
        dialogVisible: false,
        url: "api/tanzerBaseRegion",
        sort: "id,desc",
        buttonContext: "请选择省市区",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'provinceName', display_name: '省级名称' },
          { key: 'cityName', display_name: '市级名称' },
          { key: 'districtName', display_name: '区级名称' },
        ],
        elTtableColumns: [
          {
            columnProp: "provinceName",
            columnLabel: "省级名称"
          },
          {
            columnProp: "cityName",
            columnLabel: "市级名称"
          },
          {
            columnProp: "districtName",
            columnLabel: "区级名称"
          }
        ]
      },
      dialogTableDataDistributionIdTeam: {
        title: "小组分佣规则",
        dialogVisible: false,
        url: "api/tanzerBaseServeDistribution",
        sort: "id,desc",
        buttonContext: "请选择分佣规则",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'distributionName', display_name: '名称' },
          { key: 'distributionCode', display_name: '编码' },
          { key: 'type', display_name: '类型' },
        ],
        elTtableColumns: [
          {
            columnProp: "distributionName",
            columnLabel: "名称"
          },
          {
            columnProp: "distributionCode",
            columnLabel: "编码"
          },
          {
            columnProp: "type",
            columnLabel: "类型"
          }
        ]
      },
      dialogTableDataDistributionIdTeamPartner: {
        title: "合伙人分佣规则",
        dialogVisible: false,
        url: "api/tanzerBaseServeDistribution",
        sort: "id,desc",
        buttonContext: "请选择分佣规则",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'distributionName', display_name: '名称' },
          { key: 'distributionCode', display_name: '编码' },
          { key: 'type', display_name: '类型' },
        ],
        elTtableColumns: [
          {
            columnProp: "distributionName",
            columnLabel: "名称"
          },
          {
            columnProp: "distributionCode",
            columnLabel: "编码"
          },
          {
            columnProp: "type",
            columnLabel: "类型"
          }
        ]
      },
    }
  },
  watch: {
    "activityFirstFigureArr":function(val) {
      if (val) {
        this.form.activityFirstFigure = val.join(",");
      }
    },
    "activityDetailFigureArr":function(val) {
      if (val) {
        this.form.activityDetailFigure = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.id
        delete this.crud.params.typeId
        delete this.crud.params.activityCode
        delete this.crud.params.activityTitle
        delete this.crud.params.activityStatus
        delete this.crud.params.activityCost
        delete this.crud.params.activityCostType
        delete this.crud.params.areaId
        delete this.crud.params.activityTripMode
        delete this.crud.params.activityPrincipalMember
        delete this.crud.params.distributionIdTeam
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.activityFirstFigure && form.id) {
        this.activityFirstFigureArr = form.activityFirstFigure.split(",");
      }else{
        this.activityFirstFigureArr=[]
      }
      if (form.activityDetailFigure && form.id) {
        this.activityDetailFigureArr = form.activityDetailFigure.split(",");
      }else{
        this.activityDetailFigureArr=[]
      }
    },


    [CRUD.HOOK.afterToEdit](crud, form) {

      /*     //活动类型
           this.setInputValue(
             this.dialogTableDataTypeId,
             "showInputValue",
             form,
             "typeId",
             "content",
             0,
             "typeName",
             "api/tanzerBaseRegion",
             {
               page: 0,
               size: 10,
               sort: "id,desc",
               id: form.typeId
             }
           );

           //省市区数据源
           this.setInputValue(
             this.dialogTableDataAreaId,
             "showInputValue",
             form,
             "areaId",
             "content",
             0,
             "areaName",
             "api/tanzerBaseRegion",
             {
               page: 0,
               size: 10,
               sort: "id,desc",
               id: form.areaId
             }
           );

           //分佣数据源
           this.setInputValue(
             this.dialogTableDataDistributionIdTeamPartner,
             "showInputValue",
             form,
             "DistributionIdTeamPartner",
             "content",
             0,
             "distributionName",
             "api/tanzerBaseServeDistribution",
             {
               page: 0,
               size: 10,
               sort: "id,desc",
               id: form.distributionIdTeamPartner
             }
           );
           //分佣数据源
           this.setInputValue(
             this.dialogTableDataDistributionIdTeam,
             "showInputValue",
             form,
             "DistributionIdTeam",
             "content",
             0,
             "distributionName",
             "api/tanzerBaseServeDistribution",
             {
               page: 0,
               size: 10,
               sort: "id,desc",
               id: form.distributionIdTeam
             }
           );*/
    },

    //省市区数据源
    dialogTableSelectedCallBackAreaId(row) {
      this.form.areaId = row.id;
      if (this.form.areaId) {
        this.dialogTableDataAreaId.showInputValue = row.areaName;
      }
    },

    //活动类型
    dialogTableSelectedCallBackTypeId(row) {
      this.form.typeId = row.id;
      if (this.form.typeId) {
        this.dialogTableDataTypeId.showInputValue = row.typeName;
      }
    },

    dialogTableSelectedCallBackDistributionIdTeam(row) {
      this.form.distributionIdTeam = row.id;
      if (this.form.distributionIdTeam) {
        this.dialogTableDataDistributionIdTeam.showInputValue = row.distributionName;
      }
    },

    dialogTableSelectedCallBackDistributionIdTeamPartner(row) {
      this.form.distributionIdTeamPartner = row.id;
      if (this.form.distributionIdTeamPartner) {
        this.dialogTableDataDistributionIdTeamPartner.showInputValue = row.distributionName;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
